import plugin from "../../lib/plugins/plugin.js";
import { segment } from "oicq";
import puppeteer from "puppeteer";

export class example extends plugin {
    constructor() {
        super({
            name: "PxxnHub",
            dsc: "PxxnHub文本生成",
            event: "message",
            priority: 5000,
            rule: [
                {
                    reg: "#ph",
                    fnc: "PxxnHub",
                },
            ],
        });
    }

    async PxxnHub(e){
        let text1 = ""
        let text2 = ""
        const ms = e.message[0].text.slice(4)
        if(ms.length < 2 || ms.length >50){
            e.reply(`用法: #ph [Text]
        通过\"+\"标识符来换行,只会识别第一个标识符
        如果未给明标识符,会自动截取
        最少输入两个字符,最多50字
        示例: #ph Pxxn+Hub`,true)
            return
        }
        
        if(ms.indexOf("+") !== -1){
            text1 = ms.substring(0, ms.indexOf("+"))
            text2 = ms.replace(text1+"+", "")
        }else{
            text1 = ms.substring(0, Math.floor(ms.length / 2))
            text2 = ms.replace(text1, "")
        }
        const browser = await puppeteer.launch();
        const page = await browser.newPage();
        let Html = `
        <html>
        <head>
          <style>
            body {
                margin: 0;
                background-size: cover;
                float: left;
                background-color: #000
            }
            h1 {
                margin: 0;
                background-color: #000;
                padding: 20px 6px;
                font-family: Microsoft YaHei;
                color: #f0f0f0;
                float: left;
                font-size: 50px;
            }
            span {
                margin: 0;
                font-family: Microsoft YaHei;
                color: #000;
                background-color: #f90;
                padding: 10px 5px;
                border-radius: 7px;
                font-size: 50px;
            }
          </style>
        </head>
        <body>
            <h1>${text1}<span>${text2}</span></h1>
        </body>
        </html>
`       
        await page.setContent(Html)
        const body = await page.$('body');
        const boundingBox = await body.boundingBox();
        let height = Math.ceil(boundingBox.height)
        let width = Math.ceil(boundingBox.width)
        await page.setViewport({
            width: width,
            height: height,
            deviceScaleFactor: 1,
        });
        e.reply(segment.image(`base64://${
            await page.screenshot({
                encoding: 'base64'
            })}`),true)
        page.close()
        return
    }
}
